home *** CD-ROM | disk | FTP | other *** search
Wrap
/* License: This source code may not be used in other applications whether they be personal, commercial, free, or paid without written permission from Read It Later. ///////// DEVELOPER API: readitlaterlist.com/api/ ///////// If you would like to customize Read It Later or build an application that works with Read it Later take a look at the READ IT LATER OPEN API: http://readitlaterlist.com/api/ Suggestions for additions to Read It Later are VERY welcome. A large number of user suggestions have been implemented. Please let me know of any additional features you are seeking at: http://readitlaterlist.com/support/ Thanks */ Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); function RILAPIrequest() { this.keyed = true; this.APP = Components.classes['@ril.ideashower.com/rildelegate;1'].getService().wrappedJSObject; this.IO = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); this.OBS = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); this.STREAM = Components.classes["@mozilla.org/scriptableinputstream;1"]; // If you need an API key, get one for free at http://readitlaterlist.com/api/signup/ // Do not use this one, it does not have any special privileges and it is still rate-limited. // I use it to maintain usage stats, so I'd appreciate it if you didn't muddle it up :-) this.apikey = 'c4cT4A38g0G92Z820ldU509d6fp5g49b'; this.api = 'https://readitlaterlist.com/v2/'; this.apiText = 'https://text.readitlaterlist.com/v2/'; } // class definition RILAPIrequest.prototype = { // properties required for XPCOM registration: classDescription: "Read It Later API Request Javascript XPCOM Component", classID: Components.ID("{8154b020-aabf-11de-8a39-0800200c9a66}"), contractID: "@ril.ideashower.com/rilapirequest;1", QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIRILAPIRequest]), ////////////////////////////////////////// initAndStart : function(method, login, params, errorReporting, methodDescription) { this.init(method, login, params, errorReporting, methodDescription); this.start(); return this.requestId; }, init : function(method, login, params, errorReporting, methodDescription) { this.method = method; this.login = login; this.params = params; this.methodDescription = methodDescription; this.errorReporting = errorReporting ? errorReporting : 'all'; // Make request id this.requestId = this.APP.now() + this.method + this.params + Math.random(); return this.requestId; }, // -- // start : function () { try { let url; if (this.method == 'text') url = this.apiText + 'firefox'; else url = this.api + this.method; // Params let params = this.params; if (this.keyed) params += '&apikey='+this.apikey; if (this.login) { let currentLogin = this.APP.getLogin(); if (currentLogin) params += '&username='+this.APP.e(currentLogin.username)+'&password='+this.APP.e(currentLogin.password); } this.APP.d('--'); this.APP.d(url); this.APP.d(params); this.APP.d('--'); this.transport = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest); this.osc(this); this.transport.open("POST", url, true); this.transport.setRequestHeader("User-Agent" , 'Read It Later Firefox ' + this.APP.v); this.transport.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); this.transport.send(params); return; } catch(e){Components.utils.reportError(e);} // else this.transport = {status:0}; this.finished(); }, osc : function (ajaxObj) { ajaxObj.transport.onreadystatechange = function() { try { switch(ajaxObj.transport.readyState) { case(4): ajaxObj.finished(); } } catch( e ) { //server error //alert('server error'); } } }, // -- // finished : function() { try { this.success = (this.transport.status == '200'); this.response = this.transport.responseText; this.status = this.transport.status; if (!this.success) { this.error = this.header('X-Error') ? this.header('X-Error') : 'Could not reach Read It Later. Make sure you are connected to the internet.'; // Error if (this.errorReporting != 'none') { if (this.transport.status == '401') { this.APP.genericMessage('Your username and password are not correct.\nIf you recently changed your username and/or password, you will need to relogin.', [ {label:'Relogin', delegate:this.APP.getTopRIL(), selector:'relogin'}, {label:'Get Help', delegate:this.APP.getTopRIL(), selector:'getHelp'} ], false, 'Sync', true); } else { let action = this.methodDescription == 'read' ? 'getting your archive' : 'syncing'; this.APP.genericMessage('There was a problem while ' + action + ':\n'+this.error, [ {label:'Try Again', delegate:this.methodDescription == 'read' ? this.APP.getTopRIL() : this.APP.SYNC, selector: this.methodDescription == 'read' ? 'updateReadList' : 'sync'}, {label:'Get Help', delegate:this.APP.getTopRIL(), selector:'getHelp'} ], false, 'Sync', true); } } } this.OBS.notifyObservers(this, 'ril-api-request-finished', this.requestId); } catch(e) {Components.utils.reportError(e);} }, setHeader : function(name, value) { return this.transport.getResponseHeader(name); }, header : function(name) { if (this.transport) return this.transport.getResponseHeader(name); } }; var components = [RILAPIrequest]; function NSGetModule(compMgr, fileSpec) { return XPCOMUtils.generateModule(components); }